/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rcs.adslmonitor;

import java.io.InputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author ruben
 */
public abstract class ADSLStatusFromHTML extends ADSLStatus {

  static protected final char[] base64Chars = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};

  static protected String encodeBase64(String bytesToEncode) {
    StringBuilder ret = new StringBuilder(bytesToEncode.length() * 3);
    int i = 0;
    int j = 0;
    char[] charArray3 = new char[3];
    char[] charArray4 = new char[4];
    int inLen = bytesToEncode.length();

    while (inLen-- > 0) {
      charArray3[i++] = bytesToEncode.charAt(j++);
      if (i == 3) {
        charArray4[0] = (char) ((charArray3[0] & 0xfc) >> 2);
        charArray4[1] = (char) (((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4));
        charArray4[2] = (char) (((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6));
        charArray4[3] = (char) (charArray3[2] & 0x3f);

        for (i = 0; (i < 4); i++) {
          ret.append(base64Chars[charArray4[i]]);
        }
        i = 0;
      }
    }

    if (i > 0) {
      for (j = i; j < 3; j++) {
        charArray3[j] = '\0';
      }

      charArray4[0] = (char) ((charArray3[0] & 0xfc) >> 2);
      charArray4[1] = (char) (((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4));
      charArray4[2] = (char) (((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6));
      charArray4[3] = (char) (charArray3[2] & 0x3f);

      for (j = 0; (j < i + 1); j++) {
        ret.append(base64Chars[charArray4[j]]);
      }

      while ((i++ < 3)) {
        ret.append('=');
      }
    }

    return ret.toString();
  }

  public interface HttpConnectionRequest {

    public void read(InputStream in) throws Exception;
  }

  public ADSLStatusFromHTML(String routerName) {
    super(routerName);
  }

  protected boolean doConnection(String path, HttpConnectionRequest request) {
    try {
      URL url = new URL(getRouterURL(), path);
      HttpURLConnection con = (HttpURLConnection) url.openConnection();

      if (null != getUser()) {
        String userAndPassword = String.format("%s:%s", getUser(), getPassword());
        con.setRequestProperty("Authorization", String.format("Basic %s", encodeBase64(userAndPassword)));
      }

      con.connect();

      InputStream in = con.getInputStream();

      try {
        request.read(in);
      } finally {
        in.close();
      }
    } catch (ConnectException ex) {
      Logger.getLogger(ADSLStatus.class.getName()).log(Level.SEVERE, null, ex);
      setLastError(ex);
      resetUnits();
    } catch (Exception ex) {
      Logger.getLogger(ADSLStatus.class.getName()).log(Level.SEVERE, null, ex);
      setLastError(ex);
    }

    return null == getLastError();
  }

}
